package com.scitc.pleasurevideo;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.jaeger.library.StatusBarUtil;
import com.scitc.pleasurevideo.adapter.MainPagerAdapter;
import com.scitc.pleasurevideo.base.ETNoBackFragmentActivity;
import com.scitc.pleasurevideo.view.HomeViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 853
 */
public class Tabhome extends ETNoBackFragmentActivity {

    public Tabhome instance = null;
    protected ViewPager mTabPager;
    private View mTab0, mTab1, mTab2;
    private ImageView igView0, igView1, igView2;
    private TextView tView0, tView1, tView2;

    private List<Fragment> fragments;
    private MainPagerAdapter mPagerAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabhome);
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
        StatusBarUtil.setDarkMode(this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        initView();

        onClick();

    }

    /**
     * 点击事件
     */
    private void onClick() {

        mTab0.setOnClickListener(new MyOnClickListener(0));
        mTab1.setOnClickListener(new MyOnClickListener(1));
        mTab2.setOnClickListener(new MyOnClickListener(2));

        mTabPager.setAdapter(mPagerAdapter);
        mTabPager.setOffscreenPageLimit(2);
        setChingeAppbarLayout(new ChingeAppbarLayout() {
            @Override
            public void OnChingeAppbarLayout(int type) {
                //修改系统状态栏文字的颜色
                if (type != 0) {
                    StatusBarUtil.setLightMode(Tabhome.this);
                } else {
                    StatusBarUtil.setDarkMode(Tabhome.this);
                }
            }

            @Override
            public void onJumpCategoryFragment() {
                //回调跳转界面到首页
                mTabPager.setCurrentItem(0, false);
            }
        });
    }

    /**
     * 初始化数据
     */
    private void initView() {

        instance = this;

        mTabPager = findViewById(R.id.tabhomepager);

        mTabPager.addOnPageChangeListener(new MyOnPageChangeListener());

        mTab0 = findViewById(R.id.tab0);
        mTab1 = findViewById(R.id.tab1);
        mTab2 = findViewById(R.id.tab2);

        igView0 = findViewById(R.id.index_tab0_img);
        igView1 = findViewById(R.id.index_tab1_img);
        igView2 = findViewById(R.id.index_tab2_img);

        tView0 = findViewById(R.id.index_tab0_text);
        tView1 = findViewById(R.id.index_tab1_text);
        tView2 = findViewById(R.id.index_tab2_text);

        fragments = new ArrayList<>(3);

        fragments.add(HomeFragment.newInstance());
        fragments.add(CategoryFragment.newInstance());
        fragments.add(WoFragment.newInstance());

        // 填充ViewPager的数据适配器
        mPagerAdapter = new MainPagerAdapter(getSupportFragmentManager(), fragments);


    }

    /**
     * 自定义viewpage的切换
     */
    private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageSelected(int i) {
            if (i != 0) {
                StatusBarUtil.setLightMode(Tabhome.this);
            } else {
                if ("white".equals(HomeFragment.topBar)) {
                    StatusBarUtil.setDarkMode(Tabhome.this);
                }
            }


            igView0.setImageDrawable(ContextCompat.getDrawable(instance, R.mipmap.home_blur));
            igView1.setImageDrawable(ContextCompat.getDrawable(instance, R.mipmap.category_blur));
            igView2.setImageDrawable(ContextCompat.getDrawable(instance, R.mipmap.wo_blue));

            tView0.setTextColor(ContextCompat.getColor(instance, R.color.main_color_disabled));
            tView1.setTextColor(ContextCompat.getColor(instance, R.color.main_color_disabled));
            tView2.setTextColor(ContextCompat.getColor(instance, R.color.main_color_disabled));


            switch (i) {
                case 0:
                    igView0.setImageDrawable(ContextCompat.getDrawable(instance, R.mipmap.home));
                    tView0.setTextColor(ContextCompat.getColor(instance, R.color.main_color));
                    break;
                case 1:
                    igView1.setImageDrawable(ContextCompat.getDrawable(instance, R.mipmap.category));
                    tView1.setTextColor(ContextCompat.getColor(instance, R.color.main_color));
                    break;
                case 2:
                    igView2.setImageDrawable(ContextCompat.getDrawable(instance, R.mipmap.wo));
                    tView2.setTextColor(ContextCompat.getColor(instance, R.color.main_color));
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }
    }

    /**
     * 头标点击监听
     */
    private class MyOnClickListener implements View.OnClickListener {
        private int index;

        MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mTabPager.setCurrentItem(index, false);
        }
    }


    public static ChingeAppbarLayout chingeAppbarLayout;

    public interface ChingeAppbarLayout {
        void OnChingeAppbarLayout(int type);
        void onJumpCategoryFragment();
    }

    public static void setChingeAppbarLayout(ChingeAppbarLayout chingeAppbar) {
        chingeAppbarLayout = chingeAppbar;
    }

}
